package com.eva.admin.api.cms;

import com.eva.admin.biz.cms.article.CmsArticleProfileBiz;
import com.eva.admin.biz.cms.article.CmsArticleResourceBiz;
import com.eva.admin.biz.cms.article.dto.QueryCmsArticleProfileDTO;
import com.eva.admin.biz.cms.article.vo.CmsArticleProfileVO;
import com.eva.admin.biz.cms.article.vo.CmsArticleVO;
import com.eva.dao.cms.model.CmsArticle;
import com.eva.framework.cache.autowired.CleanCache;
import com.eva.framework.common.api.BaseController;
import com.eva.admin.biz.cms.article.CmsArticleBiz;
import com.eva.admin.biz.cms.article.dto.SaveCmsArticleDTO;
import com.eva.framework.oss.OSSUtil;
import com.eva.framework.rbac.authorize.ContainPermissions;
import com.eva.framework.security.prevent.PreventRepeat;
import com.eva.framework.common.model.ApiResponse;
import com.eva.framework.common.model.PageData;
import com.eva.framework.common.model.PageWrap;
import com.eva.admin.biz.cms.article.dto.QueryCmsArticleDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "文章管理")
@RestController
@RequestMapping("/cms/article")
public class CmsArticleController extends BaseController {

    @Resource
    private CmsArticleBiz cmsArticleBiz;

    @Resource
    private CmsArticleResourceBiz cmsArticleResourceBiz;

    @Resource
    private CmsArticleProfileBiz cmsArticleProfileBiz;

    @ApiOperation("上传图片")
    @PostMapping("/upload/image")
    @ContainPermissions("cms:article:publish")
    public ApiResponse<OSSUtil.UploadResult> uploadImage(MultipartFile file) {
        return ApiResponse.success(cmsArticleResourceBiz.uploadImage(file));
    }

    @ApiOperation("上传附件")
    @PostMapping("/upload/attach")
    @ContainPermissions("cms:article:publish")
    public ApiResponse<OSSUtil.UploadResult> uploadAttach(MultipartFile file) {
        return ApiResponse.success(cmsArticleResourceBiz.uploadAttach(file));
    }

    @PreventRepeat
    @ApiOperation("保存文章草稿/更新文章内容")
    @PostMapping("/save")
    @ContainPermissions("cms:article:publish")
    public ApiResponse<CmsArticle> save(@RequestBody SaveCmsArticleDTO dto) {
        return ApiResponse.success(cmsArticleBiz.save(dto));
    }

    @PreventRepeat
    @CleanCache("category:*:articles")
    @ApiOperation("发布文章")
    @GetMapping("/publish/{articleId}")
    @ContainPermissions("cms:article:publish")
    public ApiResponse<?> publish(@PathVariable Integer articleId) {
        cmsArticleBiz.publish(articleId);
        return ApiResponse.success(null);
    }

    @PreventRepeat
    @CleanCache("category:*:articles")
    @ApiOperation("上线文章")
    @GetMapping("/online/{articleId}")
    @ContainPermissions("cms:article:online")
    public ApiResponse<?> online(@PathVariable Integer articleId) {
        cmsArticleBiz.online(articleId);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量上线")
    @CleanCache("category:*:articles")
    @GetMapping("/online/batch")
    @ContainPermissions("cms:article:online")
    public ApiResponse<Integer> onlineInBatch(@RequestParam String ids) {
        return ApiResponse.success(cmsArticleBiz.onlineInBatch(this.getIdList(ids)));
    }

    @PreventRepeat
    @ApiOperation("下线文章")
    @CleanCache("category:*:articles")
    @GetMapping("/offline/{articleId}")
    @ContainPermissions("cms:article:offline")
    public ApiResponse<?> offline(@PathVariable Integer articleId) {
        cmsArticleBiz.offline(articleId);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量下线")
    @CleanCache("category:*:articles")
    @GetMapping("/offline/batch")
    @ContainPermissions("cms:article:offline")
    public ApiResponse<Integer> offlineInBatch(@RequestParam String ids) {
        return ApiResponse.success(cmsArticleBiz.offlineInBatch(this.getIdList(ids)));
    }

    @ApiOperation("删除")
    @CleanCache("category:*:articles")
    @GetMapping("/delete/{id}")
    @ContainPermissions("cms:article:delete")
    public ApiResponse<?> deleteById(@PathVariable Integer id) {
        cmsArticleBiz.deleteById(id);
        return ApiResponse.success(null);
    }

    @ApiOperation("批量删除")
    @CleanCache("category:*:articles")
    @GetMapping("/delete/batch")
    @ContainPermissions("cms:article:delete")
    public ApiResponse<Integer> deleteByIdInBatch(@RequestParam String ids) {
        return ApiResponse.success(cmsArticleBiz.deleteByIdInBatch(this.getIdList(ids)));
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    @ContainPermissions("cms:article:query")
    public ApiResponse<PageData<CmsArticleVO>> findPage (@RequestBody PageWrap<QueryCmsArticleDTO> pageWrap) {
        return ApiResponse.success(cmsArticleBiz.findPage(pageWrap));
    }

    @ApiOperation("为文章下拉选择查询概要信息列表")
    @PostMapping("/select/list")
    public ApiResponse<List<CmsArticleProfileVO>> findProfileList4Select (@RequestBody QueryCmsArticleProfileDTO dto) {
        return ApiResponse.success(cmsArticleProfileBiz.findProfileList4Select(dto));
    }
}
